﻿@using Materal.APP.Core.Models
<section class="@ClassString">
    <div class="m_select_background" @onclick="@OnBackgroundClick"></div>
    <div class="m_select" @onclick="@OnSelectClick">
        <input type="text" autocomplete="off" @bind-value="@Value.Value" readonly>
    </div>
    <div class="m_select_items">
        @foreach (KeyValueModel item in Items)
        {
            <div class="m_select_item" @onclick="@(async eventArgs=> await OnSelectItemClickAsync(eventArgs, item))">@item.Value</div>
        }
    </div>
</section>

@code {

    #region 参数
    /// <summary>
    /// 禁用状态
    /// </summary>
    [Parameter]
    public bool Disabled { get; set; }
    /// <summary>
    /// 值
    /// </summary>
    [Parameter]
    public KeyValueModel Value { get; set; }
    /// <summary>
    /// 值更改
    /// </summary>
    [Parameter]
    public EventCallback<KeyValueModel> ValueChanged { get; set; }
    /// <summary>
    /// 项
    /// </summary>
    [Parameter]
    public ICollection<KeyValueModel> Items { get; set; }
    #endregion
    #region 属性
    /// <summary>
    /// 是否显示项
    /// </summary>
    public bool IsShowItem{ get; set; }
    /// <summary>
    /// Class字符串
    /// </summary>
    public string ClassString
    {
        get
        {
            var result = "m_select_panel";
            if (IsShowItem)
            {
                result += " show";
            }
            if (Disabled)
            {
                result += " disabled";
            }
            return result;
        }
    }
    #endregion
    #region 事件
    /// <summary>
    /// 单击选择框
    /// </summary>
    /// <param name="eventArgs"></param>
    public void OnSelectClick(MouseEventArgs eventArgs)
    {
        if (Disabled) return;
        IsShowItem = !IsShowItem;
    }
    /// <summary>
    /// 单击项
    /// </summary>
    /// <param name="eventArgs"></param>
    /// <param name="item"></param>
    /// <returns></returns>
    public async Task OnSelectItemClickAsync(MouseEventArgs eventArgs, KeyValueModel item)
    {
        if (Disabled) return;
        Value = item;
        await ValueChanged.InvokeAsync(Value);
        IsShowItem = false;
    }
    /// <summary>
    /// 单击背景
    /// </summary>
    /// <param name="eventArgs"></param>
    public void OnBackgroundClick(MouseEventArgs eventArgs)
    {
        IsShowItem = false;
    }
    #endregion
}
